package com.tanhua.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.dimain.UserInfo;
import com.tanhua.server.UserInfoServer;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 杰哥不要啦~
 * @date 2021/10/27
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoServer userInfoServer;

    /**
     * 保存用户信息
     * UserInfo
     * 请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {


        //3、调用service
        userInfo.setId(UserHolder.getUserid());
        userInfoServer.save(userInfo);
        return ResponseEntity.ok(null);
    }
    /**
     * 上传用户头像
     * HeadPhoto
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {

        //2.向userinfo中设置用户id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //3.调用server
        userInfoServer.updateHead(headPhoto,UserHolder.getUserid());
        return ResponseEntity.ok(null);
    }

}
